package com.qding.park.adapter.bsg.adapter.impl;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.qding.park.adapter.bsg.adapter.config.BSGConfig;
import com.qding.park.adapter.bsg.adapter.constant.ConstantFunction;
import com.qding.park.adapter.bsg.adapter.entity.ResponseMsg;
import com.qding.park.adapter.bsg.adapter.enums.ErrorCodeEnum;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.system.NetWorkException;
import com.qding.park.common.core.network.HttpUtil;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.target.common.adapter.CarAdapter;
import com.qding.park.target.common.entity.AdCarInfo;

@Component("carAdapter_12")
public class CarAdapterImpl  implements CarAdapter {

    private static final Log _log = LogFactory.getLog(CarAdapterImpl.class);

    // private CarAdapter carAdapter;
    //
    // public CarAdapterImpl(CarAdapter carAdapter){
    // this.carAdapter=carAdapter;
    // }

    public CarAdapterImpl(String url) {
    }

    public CarAdapterImpl() {
    }

    @Override
    public AdCarInfo getMonthCardInfo(String parkId, String carNum) throws AdapterException, NetWorkException {

        String url = BSGConfig.getAdapterUrl();
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("clientID", parkId);
        jsonObj.put("cardCode", "");
        jsonObj.put("plateNo", carNum);
        jsonObj.put("flag", "2");
        Map<String, String> params = new HashMap<String, String>();
        params.put("UserID", "110");
        params.put("RequestFun", ConstantFunction.Parking_GetMonthCard);
        params.put("RequestData", jsonObj.toJSONString());
        params.put("Sign", "ddd");
        try {
            _log.info("adapter request:" + params);
            String result = HttpUtil.doPost(url, params);
            _log.info("adapter response:" + result);
            java.io.Reader in = new StringReader(result);
            Document doc = doc = (new SAXBuilder()).build(in);

            String temp = doc.getRootElement().getValue();
            in.close();
            ResponseMsg repmsg = JSONObject.parseObject(temp, ResponseMsg.class);
            String errorCode = repmsg.getErrorCode();
            if (!ErrorCodeEnum.SUCCESS.getCode().equals(errorCode)) {
                throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
                        ExceptionMessage.createExceptionMessage(
                                AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), repmsg.getErrorMsg(),
                                ConstantFunction.Parking_MonthlyFee + " adpater处理出错: " + repmsg.getErrorMsg()));
            }
            JSONObject data = (JSONObject) repmsg.getResult();
            AdCarInfo adCarInfo=new AdCarInfo();
            adCarInfo.setCharge("1".equals(data.getString("isCharge"))?true:false);
            adCarInfo.setMsg(data.getString("reason"));
            adCarInfo.setMonthFee(data.getIntValue("monthlyFee"));
            adCarInfo.setCardDescript(data.getString("cardDescript"));
            adCarInfo.setExpireDate(DateUtils.parseDate(data.getString("expireDate"), DateUtils.DATE_SHORT_DATEONLY));
//            ApUserCarInfo apUserCarInfo = new ApUserCarInfo();
//            apUserCarInfo.setIsCharge(data.getString("isCharge"));
//            apUserCarInfo.setMsg(data.getString("reason"));
//            apUserCarInfo.setMonthFee(data.getIntValue("monthlyFee"));
//            apUserCarInfo.setCardDescript(data.getString("cardDescript"));
//            apUserCarInfo
//                    .setExpireDate(DateUtils.parseDate(data.getString("expireDate"), DateUtils.DATE_SHORT_DATEONLY));
            return adCarInfo;
        } catch (AdapterException e) {
            throw e;
        } catch (NetWorkException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

//    @Override
//    public ApSyncPageInfo listUserCar(String parkCode, String sync_ID) throws AdapterException, NetWorkException {
//
//        String url = BSGConfig.getAdapterUrl();
//        JSONObject jsonObj = new JSONObject();
//        jsonObj.put("clientID", parkCode);
//        jsonObj.put("sync_ID", sync_ID);
//        Map<String, String> params = new HashMap<String, String>();
//        params.put("UserID", "110");
//        params.put("RequestFun", ConstantFunction.Parking_SyncMember);
//        params.put("RequestData", jsonObj.toJSONString());
//        params.put("Sign", "ddd");
//        _log.info("RequestData:" + jsonObj.toJSONString());
//        try {
//            _log.info("adapter request:" + params);
//            String result = HttpUtil.doPost(url, params);
//            _log.info("adapter response:" + StrUtil.decodeUnicode(result));
//            java.io.Reader in = new StringReader(result);
//            Document doc = doc = (new SAXBuilder()).build(in);
//
//            String temp = doc.getRootElement().getValue();
//            in.close();
//            ResponseMsg repmsg = JSONObject.parseObject(temp, ResponseMsg.class);
//            JSONObject data = (JSONObject) repmsg.getResult();
//            String errorCode = repmsg.getErrorCode();
//            if (!ErrorCodeEnum.SUCCESS.getCode().equals(errorCode)) {
//                throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
//                        ExceptionMessage.createExceptionMessage(
//                                AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), repmsg.getErrorMsg(),
//                                ConstantFunction.Parking_SyncMember + " adpater处理出错: " + repmsg.getErrorMsg()
//                                        + ",parkCode=" + parkCode + ",sync_ID=" + sync_ID));
//            }
//            ApSyncPageInfo pageInfo = ResponseParser.parking_SyncMember(data.getString("data"));
//
//            return pageInfo;
//        } catch (AdapterException e) {
//            throw e;
//        } catch (NetWorkException e) {
//            throw e;
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }

//    @Override
//    public List<ApUserCarInfo> allUserCar(String parkCode) throws AdapterException, NetWorkException {
//        List<ApUserCarInfo> usercarList = new ArrayList<ApUserCarInfo>();
//        ApSyncPageInfo page = listUserCar(parkCode, "-1");// 查询第一页
//        List list = page.getPageData();
//        if (!ListUtil.isEmpty(list)) {
//            usercarList.addAll(list);
//        }
//
//        while (page.getSync_Count() > 0) {
//            // 查询下一页
//            page = listUserCar(parkCode, page.getSync_ID());
//            if (!ListUtil.isEmpty(page.getPageData())) {
//                usercarList.addAll(page.getPageData());
//            }
//        }
//
//        return usercarList;
//    }

}
